<?php
require_once 'Smarty.class.php';

define ("PHP_DELIMITER_SUFFIX", "?>");
define ("PHP_DELIMITER_PREFIX", "<?");

function prefilter($source, Smarty &$smarty)
{
	$sOutput = $source;
	$sOutput = str_replace("<?/*", "{*", $sOutput);
	$sOutput = str_replace("*/?>", "*}", $sOutput);
	$sOutput = str_replace(PHP_DELIMITER_PREFIX, "{", $sOutput);
	$sOutput = str_replace(PHP_DELIMITER_SUFFIX, "}", $sOutput);
	return $sOutput;
}
/**
 * SMARTY変数クラス
 * Enter description here ...
 * @author admin
 *
 */
class View{}

/**
 * スマーティコンポ基底クラス
 * Enter description here ...
 * @author admin
 *
 */
class Smarty_Components
{
	public $smarty_template_var;
	public $view;
	protected $smarty;

	/**
	 * コンストラクタ
	 * Enter description here ...
	 * @param string $sTemplateDir
	 * @param string $sCompileDir
	 * @param string $sCompileId
	 */
	public function __construct( $sTemplateDir, $sCompileDir, $sCompileId )
	{
		$this->view = new View;
		$this->smarty = new Smarty();
		$this->smarty->register_prefilter("prefilter");
		$this->smarty->template_dir = $sTemplateDir;
		$this->smarty->compile_dir = $sCompileDir;
		$this->smarty->compile_id = $sCompileId;
		if ( !defined('RELEASED') ) {
			$this->smarty->force_compile = TRUE;
		}
	}

	/**
	 * フェッチ
	 * Enter description here ...
	 * @param unknown_type $sTemplateName
	 */
	public function fetch( $sTemplateName )
	{
		$this->_setValue();
		$this->smarty_template_var = $this->smarty->get_template_vars();
		$sResult = $this->smarty->fetch( $sTemplateName );
		return $sResult;
	}

	protected function _setValue()
	{
		$aryValueList = get_object_vars( $this->view );
		if ( $aryValueList !== FALSE ) {
			if ( $aryValueList != NULL ) {
				foreach ( $aryValueList as $key=>$value ) {
					$this->smarty->assign( $key, $value );
				}
			}
		}
	}
}

/**
 * スマーティデリミタ追加
 * Enter description here ...
 * @param unknown_type $source
 * @param unknown_type $smarty
 */
class SmartyPreFilter
{
	public function filter($source, Smarty &$smarty)
	{
		$sOutput = str_replace('great', 'bad', $source);
		return $sOutput;
	}
}
